package com.xyd.handler;

import com.xyd.common.exception.YiYunGlobalException;
import com.xyd.common.result.GlobalResultClass;
import com.xyd.common.result.ResultStatusEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public GlobalResultClass exceptionHandler(Exception e) {
        e.printStackTrace(); // 将异常信息打印至控制台
        return GlobalResultClass.fail();
    }
    // 特定异常处理
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public GlobalResultClass exceptionHandler(NullPointerException e) {
        return GlobalResultClass.fail().message(e.getMessage());
    }

    @ExceptionHandler(value = ArithmeticException.class)
    @ResponseBody
    public GlobalResultClass exceptionHandler(ArithmeticException e) {
        return GlobalResultClass.fail().message("零不可以当成被除数");
    }

    @ExceptionHandler(value = YiYunGlobalException.class)
    @ResponseBody
    public GlobalResultClass exceptionHandler(YiYunGlobalException e){
        return GlobalResultClass.fail().message(e.getMessage()).code(e.getStatus());
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseBody
    public GlobalResultClass exceptionHandler(AccessDeniedException e) throws AccessDeniedException{
        return GlobalResultClass.build(null, ResultStatusEnum.PERMISSION);
    }





}
